package org.elasticsearch.plugin.analysis.hanlp;

import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.index.analysis.AnalyzerProvider;
import org.elasticsearch.index.analysis.HanLPAnalyzerProvider;
import org.elasticsearch.index.analysis.HanLPTokenizerFactory;
import org.elasticsearch.index.analysis.TokenizerFactory;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.AnalysisPlugin;
import org.elasticsearch.plugins.Plugin;

import com.hankcs.hanlp.HanLP;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

public class AnalysisHanLPPlugin extends Plugin implements AnalysisPlugin {

	public static String PLUGIN_NAME = "analysis-hanlp";

	static {
		HanLP.Config.enableDebug(false);
		Timer timer = new Timer();
		timer.schedule(new DictReloadTask(), 0, 30000);
	}
	
	

	@Override
	public Map<String, AnalysisModule.AnalysisProvider<TokenizerFactory>> getTokenizers() {
		Map<String, AnalysisModule.AnalysisProvider<TokenizerFactory>> extra = new HashMap<>();

		extra.put("hanlp", HanLPTokenizerFactory::getHanLPTokenizerFactory);
		extra.put("hanlp_standard", HanLPTokenizerFactory::getHanLPStandardTokenizerFactory);
		extra.put("hanlp_index", HanLPTokenizerFactory::getHanLPIndexTokenizerFactory);
		extra.put("hanlp_nlp", HanLPTokenizerFactory::getHanLPNLPTokenizerFactory);
		extra.put("hanlp_n_short", HanLPTokenizerFactory::getHanLPNShortTokenizerFactory);
		extra.put("hanlp_dijkstra", HanLPTokenizerFactory::getHanLPDijkstraTokenizerFactory);
		extra.put("hanlp_crf", HanLPTokenizerFactory::getHanLPCRFTokenizerFactory);
		extra.put("hanlp_speed", HanLPTokenizerFactory::getHanLPSpeedTokenizerFactory);

		return extra;
	}

	@Override
	public Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends Analyzer>>> getAnalyzers() {
		Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends Analyzer>>> extra = new HashMap<>();

		extra.put("hanlp", HanLPAnalyzerProvider::getHanLPAnalyzerProvider);
		extra.put("hanlp_standard", HanLPAnalyzerProvider::getHanLPStandardAnalyzerProvider);
		extra.put("hanlp_index", HanLPAnalyzerProvider::getHanLPIndexAnalyzerProvider);
		extra.put("hanlp_nlp", HanLPAnalyzerProvider::getHanLPNLPAnalyzerProvider);
		extra.put("hanlp_n_short", HanLPAnalyzerProvider::getHanLPNShortAnalyzerProvider);
		extra.put("hanlp_dijkstra", HanLPAnalyzerProvider::getHanLPDijkstraAnalyzerProvider);
		extra.put("hanlp_crf", HanLPAnalyzerProvider::getHanLPCRFAnalyzerProvider);
		extra.put("hanlp_speed", HanLPAnalyzerProvider::getHanLPSpeedAnalyzerProvider);

		return extra;
	}

}
